syntax = "proto3";

package kuma.mesh.v1alpha1;

option go_package = "github.com/kumahq/kuma/api/mesh/v1alpha1";

import "api/mesh/options.proto";
import "google/protobuf/timestamp.proto";
import "validate/validate.proto";

// DataplaneInsight defines the observed state of a Dataplane.
message DataplaneInsight {

  option (kuma.mesh.resource).name = "DataplaneInsightResource";
  option (kuma.mesh.resource).type = "DataplaneInsight";
  option (kuma.mesh.resource).package = "mesh";
  option (kuma.mesh.resource).kds.send_to_global = true;
  option (kuma.mesh.resource).ws.name = "dataplane-insight";
  option (kuma.mesh.resource).ws.read_only = true;
  option (kuma.mesh.resource).scope_namespace = true;

  // List of ADS subscriptions created by a given Dataplane.
  repeated DiscoverySubscription subscriptions = 1;

  // Insights about mTLS for Dataplane.
  MTLS mTLS = 2;

  // MTLS defines insights for mTLS
  message MTLS {
    // Expiration time of the last certificate that was generated for a
    // Dataplane.
    google.protobuf.Timestamp certificate_expiration_time = 1;

    // Time on which the last certificate was generated.
    google.protobuf.Timestamp last_certificate_regeneration = 2;

    // Number of certificate regenerations for a Dataplane.
    uint32 certificate_regenerations = 3;

    // Backend that was used to generate current certificate
    string issuedBackend = 4;

    // Supported backends (CA).
    repeated string supportedBackends = 5;
  }
}

// DiscoverySubscription describes a single ADS subscription
// created by a Dataplane to the Control Plane.
// Ideally, there should be only one such subscription per Dataplane lifecycle.
// Presence of multiple subscriptions might indicate one of the following
// events:
// - transient loss of network connection between Dataplane and Control Plane
// - Dataplane restart (i.e. hot restart or crash)
// - Control Plane restart (i.e. rolling update or crash)
// - etc
message DiscoverySubscription {

  // Unique id per ADS subscription.
  string id = 1 [ (validate.rules).string.min_len = 1 ];

  // Control Plane instance that handled given subscription.
  string control_plane_instance_id = 2 [ (validate.rules).string.min_len = 1 ];

  // Time when a given Dataplane connected to the Control Plane.
  google.protobuf.Timestamp connect_time = 3
      [ (validate.rules).timestamp.required = true ];

  // Time when a given Dataplane disconnected from the Control Plane.
  google.protobuf.Timestamp disconnect_time = 4;

  // Status of the ADS subscription.
  DiscoverySubscriptionStatus status = 5
      [ (validate.rules).message.required = true ];

  // Version of Envoy and Kuma dataplane
  Version version = 6;

  // Generation is an integer number which is periodically increased by the
  // status sink
  uint32 generation = 7;
}

// DiscoverySubscriptionStatus defines status of an ADS subscription.
message DiscoverySubscriptionStatus {

  // Time when status of a given ADS subscription was most recently updated.
  google.protobuf.Timestamp last_update_time = 1;

  // Total defines an aggregate over individual xDS stats.
  DiscoveryServiceStats total = 2;

  // CDS defines all CDS stats.
  DiscoveryServiceStats cds = 3;

  // EDS defines all EDS stats.
  DiscoveryServiceStats eds = 4;

  // LDS defines all LDS stats.
  DiscoveryServiceStats lds = 5;

  // RDS defines all RDS stats.
  DiscoveryServiceStats rds = 6;
}

// DiscoveryServiceStats defines all stats over a single xDS service.
message DiscoveryServiceStats {

  // Number of xDS responses sent to the Dataplane.
  uint64 responses_sent = 1;

  // Number of xDS responses ACKed by the Dataplane.
  uint64 responses_acknowledged = 2;

  // Number of xDS responses NACKed by the Dataplane.
  uint64 responses_rejected = 3;
}

// Version defines version of Kuma Dataplane and Envoy
message Version {

  // Version of Kuma Dataplane
  KumaDpVersion kumaDp = 1;

  // Version of Envoy
  EnvoyVersion envoy = 2;

  // Versions of other dependencies, i.e. CoreDNS
  map<string, string> dependencies = 3;
}

// KumaDpVersion describes details of Kuma Dataplane version
message KumaDpVersion {

  // Version number of Kuma Dataplane
  string version = 1;

  // Git tag of Kuma Dataplane version
  string gitTag = 2;

  // Git commit of Kuma Dataplane version
  string gitCommit = 3;

  // Build date of Kuma Dataplane version
  string buildDate = 4;

  // True iff Kuma DP version is compatible with Kuma CP version
  bool kumaCpCompatible = 5;
}

// EnvoyVersion describes details of Envoy version
message EnvoyVersion {

  // Version number of Envoy
  string version = 1;

  // Full build tag of Envoy version
  string build = 2;

  // True iff Envoy version is compatible with Kuma DP version
  bool kumaDpCompatible = 3;
}
